﻿Imports Microsoft.LightSwitch.Threading
Imports System.Windows.Controls
Imports DocumentToolkit.Client

Namespace LightSwitchApplication

    Public Class PersonDetail

        Private Sub Person_Loaded(succeeded As Boolean)
            ' Write your code here.
            Me.SetDisplayNameFromEntity(Me.Person)
        End Sub

        Private Sub Person_Changed()
            ' Write your code here.
            Me.SetDisplayNameFromEntity(Me.Person)
        End Sub

        Private Sub PersonDetail_Saved()
            ' Write your code here.
            Me.SetDisplayNameFromEntity(Me.Person)
        End Sub

        Private Sub MediaCollectionAddAndEditNew_CanExecute(ByRef result As Boolean)
            ' Write your code here.

        End Sub

        Private Sub MediaCollectionAddAndEditNew_Execute()
            ' Write your code here.

            Dim file = SelectFile()
            If Not file Is Nothing Then
                Dim reader = DocumentReader.Create(file)
                Using reader
                    Dim document = Me.MediaCollection.AddNew
                    'Me.MediaProperty.FileName = file.Name
                    'Me.MediaProperty.File = reader.ToBinary
                    document.FileName = file.Name
                    document.File = reader.ToBinary
                End Using
            End If
        End Sub
        Private Function SelectFile()
            Dim result As FileInfo = Nothing
            Dispatchers.Main.Invoke(
            Sub()
                Dim dlg = New OpenFileDialog()
                If dlg.ShowDialog = True Then
                    result = dlg.File
                End If
            End Sub)

            SelectFile = result
        End Function

        Private Sub PersonDetail_Created()
            Dim Upload = Me.FindControl("Upload")
            AddHandler Upload.ControlAvailable, AddressOf Upload_ControlAvailable

        End Sub

        Private Sub Upload_ControlAvailable(sender As Object, e As ControlAvailableEventArgs)
            RemoveHandler Me.FindControl("Upload").ControlAvailable, AddressOf Upload_ControlAvailable
            Dim Button = DirectCast(e.Control, Button)
            AddHandler Button.Click, AddressOf Upload_Click

        End Sub

        Private Sub Upload_Click(sender As Object, e As System.Windows.RoutedEventArgs)
            Dim result As FileInfo = Nothing
            Dispatchers.Main.Invoke(
            Sub()
                Dim dlg = New OpenFileDialog()
                dlg.Filter = "Mutt Scout Images|*.pdf;*.xps;*.jpg;*.png"
                If dlg.ShowDialog = True Then
                    Dim file = dlg.File
                    If Not file Is Nothing Then
                        Me.Details.Dispatcher.BeginInvoke(
                            Sub()
                                'Dim reader = DocumentReader.Create(file)
                                'Using reader
                                'Dim document = Me.MediaCollection.AddNew
                                'document.FileName = file.Name
                                'document.File = reader.ToBinary

                                'If reader.RequiresConversionToXps Then
                                '' conversion needed, get the XPS binary
                                'document.FileXps = reader.ToXpsBinary
                                'End If
                                'End Using
                                'End Sub)

                                Dim bData As Byte()
                                Dim br As BinaryReader = New BinaryReader(file.OpenRead())
                                bData = br.ReadBytes(br.BaseStream.Length)
                                'Dim ms As MemoryStream = New MemoryStream(bData, 0, bData.Length)


                                Dim document = Me.MediaCollection.AddNew()
                                document.FileName = file.Name
                                document.File = bData
                                br.Close()
                            End Sub)
                    End If
                End If
            End Sub)

        End Sub

        Private Sub PersonDetail_InitializeDataWorkspace(saveChangesTo As List(Of Microsoft.LightSwitch.IDataService))
            ' Write your code here.
            If Application.User.HasPermission(Permissions.CanUpdatePeople) Then
                Me.FindControl("Upload").IsVisible = True
                Me.FindControl("MediaCollection_DeleteSelected").IsVisible = True
                Me.FindControl("Flag_AddAndEditNew").IsVisible = True
                Me.FindControl("Flag_EditSelected").IsVisible = True
                Me.FindControl("Flag_DeleteSelected").IsVisible = True
            Else
                Me.FindControl("Upload").IsVisible = False
                Me.FindControl("MediaCollection_DeleteSelected").IsVisible = False
                Me.FindControl("Flag_AddAndEditNew").IsVisible = False
                Me.FindControl("Flag_EditSelected").IsVisible = False
                Me.FindControl("Flag_DeleteSelected").IsVisible = False
            End If
        End Sub
    End Class

End Namespace